package com.huabo.system.oracle.mapper;

import com.huabo.system.oracle.entity.TblJob;
import com.hbfk.util.PageInfo;
import com.huabo.system.oracle.entity.TblRole;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;


public class TblJobDaoSqlConfig {

    public String selectListByPageInfo(PageInfo<TblJob> pageInfo, BigDecimal companyId) {
        StringBuffer sbSql = new StringBuffer("SELECT * FROM (SELECT BUDGET.*,ROWNUM RNUM FROM(SELECT * FROM TBL_JOB where COMPANYID="+companyId+"");
        sbSql.append(") BUDGET WHERE ROWNUM <= "+(pageInfo.getCurrentPage()*pageInfo.getPageSize())+" ) WHERE RNUM > "+pageInfo.getCurrentRecord());
        return sbSql.toString();
    }

    public String saveJob(TblJob job) {
        StringBuffer column = new StringBuffer("INSERT INTO TBL_JOB (JOBID");
        StringBuffer value = new StringBuffer(" VALUES (HIBERNATE_SEQUENCE.nextval");

        if(job.getJobname() != null) {
            column.append(",JOBNAME");
            value.append(",'"+job.getJobname()+"'");
        }
        if(job.getCompanyId() != null) {
            column.append(",COMPANYID");
            value.append(",'"+job.getCompanyId()+"'");
        }
        if(job.getHistorycode() != null) {
            column.append(",historycode");
            value.append(",'"+job.getHistorycode()+"'");
        }
        if(job.getDatasource() != null) {
            column.append(",datasource");
            value.append(",'"+job.getDatasource()+"'");
        }
        if(job.getStatus() != null) {
            column.append(",status");
            value.append(",'"+job.getStatus()+"'");
        }
        if(job.getCode() != null) {
            column.append(",code");
            value.append(",'"+job.getCode()+"'");
        }
        if(job.getCategoryId() != null) {
            column.append(",categoryId");
            value.append(",'"+job.getCategoryId()+"'");
        }
        if(job.getCategoryName() != null) {
            column.append(",categoryName");
            value.append(",'"+job.getCategoryName()+"'");
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(job.getCreateTime() != null) {
            column.append(",CreateTime");
            value.append(",to_date('"+sdf.format(job.getCreateTime())+"','yyyy-MM-dd')");
        }
        if(job.getDescription() != null) {
            column.append(",description");
            value.append(",'"+job.getDescription()+"'");
        }
        column.append(")");
        value.append(")");
        String sql = column.toString()+value.toString();
        return sql;
    }

    public String updateJob(TblJob newJob) {
        StringBuffer sql = new StringBuffer("UPDATE TBL_JOB SET ");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        if(newJob.getJobname() != null && !"".equals(newJob.getJobname())) {
            sql.append("  JOBNAME = '"+newJob.getJobname()+"'");
        }
        if(newJob.getCompanyId() != null && !"".equals(newJob.getCompanyId())) {
            sql.append(" , COMPANYID = '"+newJob.getCompanyId()+"'");
        }
        
        if(newJob.getHistorycode() != null && !"".equals(newJob.getHistorycode())) {
            sql.append(" , historycode = '"+newJob.getHistorycode()+"'");
        }
        if(newJob.getDatasource() != null && !"".equals(newJob.getDatasource())) {
            sql.append(" , datasource = '"+newJob.getDatasource()+"'");
        }
        
        if(newJob.getStatus() != null && !"".equals(newJob.getStatus())) {
            sql.append(" , status = '"+newJob.getStatus()+"'");
        }
        if(newJob.getCode() != null && !"".equals(newJob.getCode())) {
            sql.append(" , code = '"+newJob.getCode()+"'");
        }
        if(newJob.getCategoryId() != null && !"".equals(newJob.getCategoryId())) {
            sql.append(" , categoryId = '"+newJob.getCategoryId()+"'");
        }
        if(newJob.getCategoryName() != null && !"".equals(newJob.getCategoryName())) {
            sql.append(" , categoryName = '"+newJob.getCategoryName()+"'");
        }
        if(newJob.getCreateTime() != null && !"".equals(newJob.getCreateTime())) {
            sql.append(" , CreateTime = to_date('"+sdf.format(newJob.getCreateTime())+"','yyyy-MM-dd')'");
        }
        
        if(newJob.getDescription() != null && !"".equals(newJob.getDescription())) {
            sql.append(" , description = '"+newJob.getDescription()+"'");
        }
        sql.append(" WHERE JOBID = '"+newJob.getJobid()+"'");
        return sql.toString();
    }


}